package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.StrategyCatalog;
import cn.wolfcode.trip.base.domain.TravelCommend;
import cn.wolfcode.trip.base.mapper.StrategyMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.query.TravelCommendQueryObject;
import cn.wolfcode.trip.base.service.IStrategyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class StrategyServiceImpl implements IStrategyService {

    @Autowired
    private StrategyMapper strategyMapper;

    @Override
    public void saveOrUpdate(Strategy strategy) {
        if (strategy.getId() != null) {
            strategyMapper.updateByPrimaryKey(strategy);
        } else {
            strategyMapper.insert(strategy);
        }
    }

    @Override
    public Strategy getById(Long id) {
        return strategyMapper.selectByPrimaryKey(id);
    }

    @Override
    public void addFavorite(Long strategyId, Long userId) {
        strategyMapper.addFavorite(strategyId,userId);
        strategyMapper.updateAddFavoriteCount(strategyId);
    }

    @Override
    public void removeFavorite(Long strategyId, Long userId) {
        strategyMapper.removeFavorite(strategyId,userId);
        strategyMapper.updateRemoveFavoriteCount(strategyId);
    }

    @Override
    public void checkFavorite(Long strategyId, Long userId) {
        Strategy myFavoriteStrategy=strategyMapper.selectMyFavorite(strategyId,userId);
        if (myFavoriteStrategy == null) {
            throw new RuntimeException("没收藏");
        }
    }

    @Override
    public void updateReadCount(Long strategyId) {
        strategyMapper.updateReadCount(strategyId);
    }

    @Override
    public void checkTravelList(Long strategyId, Long userId) {
        Strategy travelList=strategyMapper.selectTravelList(strategyId,userId);
        if (travelList == null) {
            throw new RuntimeException("没加入旅行单");
        }
    }

    @Override
    public void addTravelList(Long strategyId, Long userId) {
        Date createTime = new Date();
        strategyMapper.addTravelList(strategyId,userId,createTime);
    }

    @Override
    public void removeTravelList(Long strategyId, Long userId) {
        Date createTime = new Date();
        strategyMapper.removeTravelList(strategyId,userId);
    }

    @Override
    public List<Strategy> listAll(Integer state) {
        return strategyMapper.selectAll(state);
    }


    @Override
    public PageInfo query(StrategyQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Strategy> list = strategyMapper.selectForList(qo);
        return new PageInfo(list);
    }

}
